<template>
  <q-page class="cc-admin">
    <div class="col bg-white shadow-2 q-pa-md q-ma-sm">
      <q-table flat color="primary" class="cross_table" separator="cell" :columns="columns" :data="list" row-key="id" :pagination.sync="pagination" :visible-columns="group" @request="query" :rows-per-page-options="[10, 20, 50, 100]" selection="multiple" :selected.sync="selected">
        <template v-slot:top="">
          <div class="row no-wrap full-width">
            <q-input clearable outlined dense placeholder="请输入关键字搜索" class="on-left" @input="query" debounce="500" v-model="key">
              <template #append>
                <q-btn flat round icon="search" color="primary" @click="query" :loading="loading">
                  <q-tooltip>搜索</q-tooltip>
                </q-btn>
              </template>
            </q-input>
            <q-space />
          </div>
        </template>
        <template #body-cell-opt="props">
          <q-td :props="props" :auto-width="true">
            <q-btn flat round dense color="primary" icon="mdi-file-search-outline" @click="edit(props.row)">
              <q-tooltip>查看</q-tooltip>
            </q-btn>
            <btn-del label="试卷" @confirm="del(props.row)" />
          </q-td>
        </template>
      </q-table>
    </div>

    <q-dialog maximized flat persistent ref="dialog">
      <q-form class="dialog_card column">
        <h5 class="view_title justify-between q-px-md">
          查看试卷
          <q-btn dense outline round icon="clear" size="sm" v-close-popup />
        </h5>
        <q-scroll-area class="col">
          <div class="row q-col-gutter-x-md dialog_form q-pa-md">
            <div class="col-12">
              <h5>一、选择题</h5>
              <!-- <q-input outlined dense v-model="" type="text" /> -->
            </div>
            <div class="col-12 q-pb-md" v-for="(item, index) in this.form.choiceQuestionsList" :key="item.id">
              <div v-html="renderMath(index + 1 + '.' + item.question)"></div>
              <div class="q-video" style="margin-top: 10px" v-if="item.viedoUrl">
                <viewer class="label-list q-mb-sm" style="max-height: 100px" ref="viewer" :options="options" :image="item.viedoUrl" v-if="item.viedoUrl" @inited="inited">
                  <img class="q-pl-sm" :src="item.viedoUrl" style="object-fit: contain; max-height: 100%; max-width: 150px" />
                </viewer>
              </div>
              <q-radio v-model="form.choiceAnswer[index]" color="cyan" val="A" @input="changeCheck({ value: 'A', index })">
                <template v-slot:default>
                  <div v-html="renderMath('A: ' + item.optionA)"></div>
                </template>
              </q-radio>
              <q-radio v-model="form.choiceAnswer[index]" color="cyan" val="B" @input="changeCheck({ value: 'B', index })">
                <template v-slot:default>
                  <div v-html="renderMath('B: ' + item.optionB)"></div>
                </template>
              </q-radio>
              <q-radio v-model="form.choiceAnswer[index]" color="cyan" val="C" @input="changeCheck({ value: 'C', index })">
                <template v-slot:default>
                  <div v-html="renderMath('C: ' + item.optionC)"></div>
                </template>
              </q-radio>
              <q-radio v-model="form.choiceAnswer[index]" color="cyan" val="D" @input="changeCheck({ value: 'D', index })">
                <template v-slot:default>
                  <div v-html="renderMath('D: ' + item.optionD)"></div>
                </template>
              </q-radio>
              <div class="answerdiv q-pl-xl">
                <!-- <q-input outlined dense v-model="form.choiceAnswer[index]" type="text" class="answer" /> -->
                <q-icon name="mdi-check-bold" color="green" size="md" v-show="item.answer == form.choiceAnswer[index]"></q-icon>
                <q-icon name="mdi-close" color="red" size="md" v-show="item.answer != form.choiceAnswer[index]"></q-icon>
              </div>
              <!-- 答案解析条件需要改为作答完毕 -->
              <h5 v-if="editType === '答题'" class="text-red-6 blankAnalysis" v-html="renderMath(item.analysis)"></h5>
            </div>
            <div>
              <div class="col-12">
                <h5>二、填空题</h5>
                <!-- <q-input outlined dense v-model="" type="text" /> -->
              </div>
              <div class="col-12 q-pb-md" v-for="(item, index) in this.form.blankQuestionsList" :key="item.id">
                <div class="blankAnswerdiv">
                  <div>
                    <div v-html="renderMath(index + 1 + '.' + item.question)" style="line-height: 300%"></div>
                    <div class="q-video col-12" style="margin-top: 10px" v-if="item.viedoUrl">
                      <viewer class="label-list q-mb-sm" style="max-height: 100px" ref="viewer" :options="options" :image="item.viedoUrl" v-if="item.viedoUrl" @inited="inited">
                        <img class="q-pl-sm" :src="item.viedoUrl" style="object-fit: contain; max-height: 100%; max-width: 150px" />
                      </viewer>
                    </div>
                  </div>
                  <div class="q-pl-xl" style="display:inline">
                    <q-input readonly outlined dense v-model="form.blankAnswer[index]" type="text" class="answer" />

                    <!-- <q-icon name="mdi-check-bold" color="green" size="md" v-show="item.answer == form.choiceAnswer[index]"></q-icon> -->
                    <!-- <q-icon name="mdi-close" color="red" size="md" v-show="item.answer != form.choiceAnswer[index]"></q-icon> -->
                  </div>
                  <div>
                    <div class="q-gutter-sm">
                      <q-radio disable v-model="form.blankResult[index]" checked-icon="mdi-check" unchecked-icon="" val="1" label="对"></q-radio>
                      <q-radio disable v-model="form.blankResult[index]" checked-icon="mdi-check" unchecked-icon="" val="0" label="错" />
                    </div>
                  </div>
                </div>
                <!-- 答案解析条件需要改为作答完毕 -->
                <div v-if="editType === '答题'" class="text-red-6 blankAnalysis" v-html="renderMath(item.analysis)"></div>
              </div>
            </div>
            <div class="col-12">
              <h6>三、应用题</h6>
              <!-- <q-input outlined dense v-model="" type="text" /> -->
            </div>
            <div class="col-12 q-pb-md q-mb-xl" style="" v-for="(item, index2) in this.form.designAnswer" :key="item.id">
              <div style="font-weight: 800; font-size: 16px" v-html="renderMath(index2 + 1 + '.' + item.questionStem)"></div>
              <div class="q-video" style="margin-top: 10px" v-if="item.viedoUrl">
                <viewer class="label-list q-mb-sm" style="max-height: 100px" ref="viewer" :options="options" :image="item.viedoUrl" v-if="item.viedoUrl" @inited="inited">
                  <img class="q-pl-sm" :src="item.viedoUrl" style="object-fit: contain; max-height: 100%; max-width: 150px" />
                </viewer>
              </div>

              <div class="col-12" style=" border: 1px solid black">
                <q-splitter v-model="splitterModel">
                  <template v-slot:before>
                    <q-tabs v-model="tab2[index]" vertical class="text-teal">
                      <q-tab :name="item2.indexNum + item2.question" icon="" :label="`第${index + 1}题`" v-for="(item2, index) in item.questionList" :key="item2.id" />
                    </q-tabs>
                  </template>

                  <template v-slot:after>
                    <q-tab-panels v-model="tab2[index]" animated swipeable vertical transition-prev="jump-up" transition-next="jump-up">
                      <q-tab-panel :name="item3.indexNum + item3.question" v-for="item3 in item.questionList" :key="item3.id">
                        <div>
                          <div style="display: flex; justify-content: space-between">
                            <div class="col-12">
                              <!-- <h5>题目：</h5> -->
                              <div outlined readonly dense v-html="renderMath(item3.question)" type="textarea" />
                            </div>
                            <div class="q-gutter-sm">
                              <q-radio v-model="item3.shape" checked-icon="mdi-check" unchecked-icon="" val="1" label="对"></q-radio>
                              <q-radio v-model="item3.shape" checked-icon="mdi-check" unchecked-icon="" val="0" label="错" />
                            </div>
                          </div>
                          <div class="col-12">
                            <h5>你的答案：</h5>
                            <q-input outlined dense v-model="item3.answer" readonly type="textarea" :rows="15" />
                            <div class="img" style="margin-top: 10px" v-if="item3.viedoUrl !== ''">
                              <viewer class="label-list q-mb-sm" style="max-height: 150px" ref="viewer" :options="options" :image="item3.viedoUrl" v-if="item3.viedoUrl" @inited="inited">
                                <img class="q-pl-sm" :src="item3.viedoUrl" style="object-fit: contain; max-height: 100%; max-width: 150px" />
                              </viewer>
                            </div>
                          </div>
                          <div class="col-12">
                            <h5>实际答案：</h5>
                            <div outlined readonly dense v-html="renderMath(item3.realAnswer)" type="textarea" />
                          </div>

                          <div class="col-12">
                            <div class="blankAnalysis text-red" outlined readonly dense v-html="renderMath(item3.analysis) || '无'" type="textarea" />
                          </div>

                          <div style="display: flex; align-items: center">
                            <div class="q-video" style="margin-top: 10px" v-if="imageExtensions.includes(item3.analysisVideo.substring(item3.analysisVideo.lastIndexOf('.')))">
                              <viewer class="label-list q-mb-sm" style="max-height: 100px" ref="viewer" :options="options" :image="item3.analysisVideo" v-if="item3.analysisVideo" @inited="inited">
                                <img class="q-pl-sm" :src="item3.analysisVideo" style="object-fit: contain; max-height: 100%; max-width: 150px" />
                              </viewer>
                            </div>
                            <div class="q-video" style="margin-top: 10px" v-else>
                              <iframe :src="item3.analysisVideo" frameborder="0" allowfullscreen height="100%" :ratio="4 / 3" />
                            </div>
                          </div>
                        </div>
                      </q-tab-panel>
                    </q-tab-panels>
                  </template>
                </q-splitter>
              </div>
              <!-- 答案解析条件需要改为作答完毕 -->
              <h5 v-if="editType === '答题'" class="text-red-6">{{ item.analysis }}</h5>
            </div>
          </div>
        </q-scroll-area>
        <div class="row justify-center q-pa-md">
          <q-btn outline color="primary" icon="mdi-close-thick" label="关闭" v-close-popup />
          <!-- <q-btn class="q-mx-md" color="primary" icon="mdi-check-bold" label="提交" :loading="loading" type="submit2" /> -->
        </div>
      </q-form>
    </q-dialog>
    <confirm ref="confirmDialog" :msg="confirmMsg" @confirm="deleteBatch()" />
    <q-inner-loading :showing="loading">
      <q-spinner-gears size="50px" color="primary" />
    </q-inner-loading>
  </q-page>
</template>

<script>
import { IndexMixin } from 'boot/mixins';
import { getDictLabel } from 'boot/dictionary';
import confirm from 'components/confirm';
import 'katex/dist/katex.min.css';
import katex from 'katex';
import { before } from 'lodash';

export default {
  name: 'MathViewPaper',
  mixins: [IndexMixin],
  components: {
    confirm,
  },
  data() {
    return {
      catalog: '02',
      tab: '0101',
      tab2: [],

      index: 0,
      splitterModel: 20,
      form: {
        id: '',
        paperName: '',
        choiceQuestionsList: [],
        choiceAnswer: [],
        designAnswer: [],
        blankAnswer: [],
        blankQuestionsList: [],
        blankResult: [],
      },
      columns: [
        {
          name: 'index',
          align: 'center',
          label: '序号',
          field: 'index',
        },
        {
          name: 'paperName',
          align: 'center',
          label: '试卷名称',
          field: 'paperName',
        },
        {
          name: 'opt',
          align: 'center',
          label: '操作',
          field: 'id',
        },
      ],
      // examForm: {
      //   id: '',
      //   choiceIds: '',
      //   paperName: '',
      //   designQuestionsListDtoList: [],
      //   designAnswer: [],
      //   choiceQuestionsList: [],
      //   choiceAnswer: [],
      // },
      showQuery: true,
      headers: [{ name: 'Authorization', value: localStorage.Authorization }],
      uploadUrl: `${process.env.SERVER_URL}${process.env.BASE_URL}/sys/common/upload`,
      imgUrl: `${process.env.SERVER_URL}${process.env.BASE_URL}/sys/common/static`,
      url: {
        list: '/exam/paper/viewList2',
      },
    };
  },
  methods: {
    // 初始化预览组件
    inited(viewer) {
      this.$viewer = viewer;
    },
    renderMath(str) {
      return katex.renderToString(str, {
        throwOnError: false,
      });
    },
    getDictLabel,
    initDict() {},
  },
  mounted() {
    this.initDict();
  },
};
</script>

<style lang="stylus">
.answerdiv
  white-space normal
  word-wrap break-word
  overflow-wrap break-word
  display inline-block
  .answer
    display inline-flex
    max-width 500px
.blankAnalysis::before
  content '解析：'
  white-space pre-wrap
.blankAnswerdiv
  display flex
  flex-direction row
  .answer
    display inline-flex
    max-width 500px
</style>
